//
//  SettingViewController.m
//  messageManager
//
//  Created by desunire on 2017/8/28.
//  Copyright © 2017年 desunire. All rights reserved.
//

#import "SettingViewController.h"
#import "LMJDropdownMenu.h"
@interface SettingViewController ()<LMJDropdownMenuDelegate,SCJDropDownMultiSelectMenuDelegate,scjMulateChooseViewDelegate>

@property(strong,nonatomic)NSMutableArray *pushType;//推送类型

//视图
@property(strong,nonatomic)SCJDropDownMultiSelectMenu * dropdownMenu;


@property(strong,nonatomic)NSArray *chooseServiety;//选中的严重性


@property(strong,nonatomic)SCJMulateChooseView *chooseView;

//筛选数据源
@property(strong,nonatomic)NSMutableDictionary *serviceDic;


@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //self.pushType=[NSMutableArray arrayWithArray:@[@"全部",@"严重",@"重大",@"轻微",@"警告",@"正常",@"未知"]];
    
    self.serviceDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"0",@"critical",@"0",@"major",@"0",@"minor",@"0",@"warning",@"0",@"normal",@"0",@"unknown", nil];
    
    [self setChooseType];
    
    self.quitBtn.backgroundColor = navColor;
    self.quitBtn.layer.masksToBounds=YES;
    self.quitBtn.layer.cornerRadius=5;
    
}


-(void)setChooseType{
    //获取缓存的设置推送类型
    NSString *pushType = [[NSUserDefaults standardUserDefaults] valueForKey:@"pushType"];
    //    if ([pushType rangeOfString:@"all"].location == NSNotFound) {
    //
    //    }
    NSLog(@"%@",pushType);
    if ([pushType containsString:@"all"]) {
        [self.serviceDic setObject:@"1" forKey:@"all"];
    }else{
       [self.serviceDic setObject:@"0" forKey:@"all"];
    }
    if ([pushType containsString:@"critical"]) {
        [self.serviceDic setObject:@"1" forKey:@"critical"];
    }else{
        [self.serviceDic setObject:@"0" forKey:@"critical"];
    }
    if ([pushType containsString:@"major"]) {
        [self.serviceDic setObject:@"1" forKey:@"major"];
    }else{
        [self.serviceDic setObject:@"0" forKey:@"major"];
    }
    if ([pushType containsString:@"minor"]) {
        [self.serviceDic setObject:@"1" forKey:@"minor"];
    }else{
        [self.serviceDic setObject:@"0" forKey:@"minor"];
    }
    if ([pushType containsString:@"normal"]) {
        [self.serviceDic setObject:@"1" forKey:@"normal"];
    }else{
        [self.serviceDic setObject:@"0" forKey:@"normal"];
    }
    if ([pushType containsString:@"unknown"]) {
        [self.serviceDic setObject:@"1" forKey:@"unknown"];
    }else{
        [self.serviceDic setObject:@"0" forKey:@"unknown"];
    }
    if ([pushType containsString:@"warning"]) {
        [self.serviceDic setObject:@"1" forKey:@"warning"];
    }else{
        [self.serviceDic setObject:@"0" forKey:@"warning"];
    }
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}





-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];
    
    // 导航栏标题字体颜色
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:19],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    // 导航栏左右按钮字体颜色
    
    self.navigationController.navigationBar.tintColor =
    [UIColor whiteColor];
    
    self.navigationItem.title = @"设置";
}

-(void)setPushTypeNet:(NSInteger)index{
    
    /**
     pushType：
     all 全部
     critical 严重
     major 重大
     minor 轻微
     warning 警告
     normal 正常
     unknown 未知
    */
    NSUserDefaults *userDefault=[NSUserDefaults standardUserDefaults];
    NSString *name=[userDefault valueForKey:@"name"];
    NSString *pwd=[userDefault valueForKey:@"pwd"];
    NSString *pushType;
    if (index==0) {
        pushType=@"all";
    }if (index==1) {
        pushType=@"critical";
    }if (index==2) {
        pushType=@"major";
    }if (index==3) {
        pushType=@"minor";
    }if (index==4) {
        pushType=@"warning";
    }if (index==5) {
        pushType=@"normal";
    }if (index==6) {
        pushType=@"unknown";
    }
    [SVProgressHUD show];
    NSDictionary *pare=@{@"loginName":name,@"password":pwd,@"pushType":pushType};
    [[YCJHttpRequest sharedInstance] setPushType:pare complete:^(NSError *error, BOOL isSuccess) {
        [SVProgressHUD dismiss];
        if (isSuccess) {
            //本地缓存上次选择的推送类型
            [[NSUserDefaults standardUserDefaults] setObject:[self.pushType objectAtIndex:index] forKey:@"pushType"];
            [[NSUserDefaults standardUserDefaults] synchronize];
        }else{
            if (error) {
                [self show:@"网络异常,操作失败"];
            }else{
                [self show:@"设置失败"];
            }
        }
    }];
}

//登录页面提示
-(void)show:(NSString *)title{
    [SVProgressHUD showWithStatus:title];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
    });
}

#pragma mark - LMJDropdownMenu Delegate

- (void)dropdownMenu:(LMJDropdownMenu *)menu selectedCellNumber:(NSInteger)number{
    NSLog(@"你选择了：%ld",number);
    //设置推送类型
    [self setPushTypeNet:number];
}

- (void)dropdownMenuWillShow:(LMJDropdownMenu *)menu{
    NSLog(@"--将要显示--");
}
- (void)dropdownMenuDidShow:(LMJDropdownMenu *)menu{
    NSLog(@"--已经显示--");
}

- (void)dropdownMenuWillHidden:(LMJDropdownMenu *)menu{
    NSLog(@"--将要隐藏--");
}
- (void)dropdownMenuDidHidden:(LMJDropdownMenu *)menu{
    NSLog(@"--已经隐藏--");
}
//退出登录
- (IBAction)quitLoad:(id)sender {
    
    //弹出确认框
    UIAlertController *vc=[UIAlertController alertControllerWithTitle:@"提示" message:@"是否确认退出登录?" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *sure=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [JPUSHService setTags:nil alias:@"" fetchCompletionHandle:^(int iResCode, NSSet *iTags, NSString *iAlias) {
            NSLog(@"绑定和解绑rescode: %d, \ntags: %@, \nalias: %@\n", iResCode, iTags , iAlias);
        }];
        
        NSUserDefaults *userDefault=[NSUserDefaults standardUserDefaults];
        [userDefault removeObjectForKey:@"pwd"];
        UIStoryboard * main=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
        ViewController *vcc =[main instantiateViewControllerWithIdentifier:@"loadView"];
        [self.navigationController pushViewController:vcc animated:YES];
    }];
    
    UIAlertAction *cancel =[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [vc addAction:cancel];
    [vc addAction:sure];
    
    [self presentViewController:vc animated:YES completion:nil];
   
}
- (IBAction)setWarningServiceType:(id)sender {
    [self setChooseType];
    self.chooseView = [[SCJMulateChooseView alloc] initWithFrame:self.view.frame andData:self.serviceDic andTitle:nil];
    self.chooseView.delegate = self;
    [self.view addSubview:self.chooseView];
}


//隐藏选择框
-(void)hideChooseView{
    [self.chooseView removeFromSuperview];
    self.chooseView = nil;
    
}

-(void)selectedCellIndex:(NSMutableDictionary *)chooseDic{
    [SVProgressHUD show];
    
    NSUserDefaults *userDefault=[NSUserDefaults standardUserDefaults];
    NSString *name=[userDefault valueForKey:@"name"];
    NSString *pwd=[userDefault valueForKey:@"pwd"];
    NSString *pushType=@"";
    
    NSArray *keysArr =[chooseDic allKeys];
    for (int i=0; i<keysArr.count; i++) {
        NSString *key =[keysArr objectAtIndex:i];
        NSString *value = [chooseDic valueForKey:key];
        if ([key isEqualToString:@"all"]) {
            if ([value integerValue] == 1) {
                pushType = [pushType stringByAppendingString:@"all"];
                pushType = [pushType stringByAppendingString:@","];
            }
        }else if ([key isEqualToString:@"critical"]) {
            if ([value integerValue] == 1) {
                pushType = [pushType stringByAppendingString:@"critical"];
                pushType = [pushType stringByAppendingString:@","];
            }
        }else if ([key isEqualToString:@"major"]) {
            if ([value integerValue] == 1) {
                pushType = [pushType stringByAppendingString:@"major"];
                pushType = [pushType stringByAppendingString:@","];
            }
        }else if ([key isEqualToString:@"minor"]) {
            if ([value integerValue] == 1) {
                pushType = [pushType stringByAppendingString:@"minor"];
                pushType = [pushType stringByAppendingString:@","];
            }
        }else if ([key isEqualToString:@"normal"]) {
            if ([value integerValue] == 1) {
                pushType = [pushType stringByAppendingString:@"normal"];
                pushType = [pushType stringByAppendingString:@","];
            }
        }else if ([key isEqualToString:@"unknown"]) {
            if ([value integerValue] == 1) {
                pushType = [pushType stringByAppendingString:@"unknown"];
                pushType = [pushType stringByAppendingString:@","];
            }
        }else if ([key isEqualToString:@"warning"]) {
            if ([value integerValue] == 1) {
                pushType = [pushType stringByAppendingString:@"warning"];
                pushType = [pushType stringByAppendingString:@","];
            }
        }
        
    }
    if (pushType.length == 0) {
        //什么都没设置
        [self show:@"至少选择一个告警级别"];
    }else{
        pushType = [pushType substringToIndex:pushType.length-1];
        NSLog(@"type::%@",pushType);
        NSDictionary *pare=@{@"loginName":name,@"password":pwd,@"pushType":pushType};
        [[YCJHttpRequest sharedInstance] setPushType:pare complete:^(NSError *error, BOOL isSuccess) {
            [SVProgressHUD dismiss];
            if (isSuccess) {
                //本地缓存上次选择的推送类型
                [[NSUserDefaults standardUserDefaults] setObject:pushType forKey:@"pushType"];
                [[NSUserDefaults standardUserDefaults] synchronize];
                //[self show:@""];//设置成功
                [self hideChooseView];
            }else{
                if (error) {
                    [self show:@"网络异常,操作失败"];
                    [self hideChooseView];
                }else{
                    [self show:@"设置失败"];
                    [self hideChooseView];
                }
            }
        }];
    }
    
}
@end
